package com.fatkun;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.jdo.PersistenceManager;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.*;

import com.fatkun.PMF;
import com.fatkun.UserData;

@SuppressWarnings("serial")
public class MailHandlerServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);
		try {
			MimeMessage message = new MimeMessage(session, req.getInputStream());
			InternetAddress FromUserAdd[] = (InternetAddress[]) message
					.getFrom();
			String FromUser = FromUserAdd[0].getAddress();
			// MimeMessage MailPart = (MimeMessage) message.getContent();
			Multipart mp = (Multipart) message.getContent();
			BodyPart part;
			// Multipart的数量,用于除了多个part,比如多个附件
			// for(int m=0;m<mpCount-1;m++)
			// {
			part = mp.getBodyPart(0);
			// disposition=part.getDisposition();
			// if(disposition!=null && disposition.equals(Part.ATTACHMENT))
			// 判断是否有附件
			// {
			// this.saveAttach(part);
			// 这个方法负责保存附件，
			// }else{
			String sBodyData = part.getContent().toString();
			// System.out.println(sBodyData);
			// }

			if (-1 != message.getSubject().toString().indexOf("New")) {
				PersistenceManager pm = PMF.get().getPersistenceManager();
				String query = "select from " + UserData.class.getName();
				List<UserData> userdatas = (List<UserData>) pm.newQuery(query)
						.execute();
				for (UserData u : userdatas) {
					message.addRecipients(Message.RecipientType.TO,InternetAddress.parse(u.getUserEmail()));
				}
				pm.close();
				message.setSubject("更新说明");
				message.setText(sBodyData);

			} else {
				// get data
				sBodyData = sBodyData.trim();
				Double max = Double.valueOf(sBodyData.substring(0, 4));
				sBodyData = sBodyData.substring(5, sBodyData.length());
				sBodyData = sBodyData.trim();
				Double min = Double.valueOf(sBodyData.substring(0, 4));

				// save data for each email
				PersistenceManager pm = PMF.get().getPersistenceManager();
				String query = "select from " + UserData.class.getName();
				List<UserData> userdatas = (List<UserData>) pm.newQuery(query)
						.execute();
				if (userdatas.isEmpty()) {
					UserData userdata = new UserData(FromUser, max, min, 0);
					pm.makePersistent(userdata);

				} else {
					for (UserData u : userdatas) {
						if (0 == u.getUserEmail().compareToIgnoreCase(FromUser)) {
							pm.deletePersistent(u);
						}
					}
					UserData userdata = new UserData(FromUser, max, min, 0);
					pm.makePersistent(userdata);

				}
				pm.close();

				// sendback
				
				message.addRecipient(Message.RecipientType.TO,
						new InternetAddress(FromUser, "Mr.Silver"));
				message.setSubject("FeedBack");
				message.setText("OK,I get it");
				
			}
			message.setFrom(new InternetAddress("liyun8552@gmail.com",
						"Mr Admin"));
			Transport.send(message);

			// }
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
		}

	}
}
